//表格初始化配置
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'header',
            title: '作者头像',
            align: 'center',
            formatter: function (value) {
                if (!empty(value)) {
                    var img_html = "<a href='" + value + "' title='点击查看大图' target='_blank'>" +
                        "<img src='" + value + "' width='40' onerror='imgNotfind(event);' /></a>";
                    return img_html;
                } else {
                    return "<img src='" + value + "' width='40' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'username',
            title: '作者名字',
            align: 'center'
        },
        {
            field: 'content',
            title: '内容',
            align: 'center'
        },
        {
            field: 'imgShowUrls',
            title: '上传的图片',
            align: 'center',
            width: 220,
            formatter: function (value) {
                if (!empty(value)) {
                    var html = "";
                    var jsonObj = JSON.parse(value);
                    for (var i = 0; i < jsonObj.length; i++) {
                        html += "<a href='" + jsonObj[i] + "' title='点击查看大图' target='_blank'>" +
                            "<img src='" + jsonObj[i] + "' width='60' style='margin-right: 5px;border: 1px solid #ddd' onerror='imgNotfind(event);' /></a>"
                    }
                    return html;
                } else {
                    return "<img src='" + value + "' width='60' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'typeName',
            title: '话题',
            align: 'center'
        },
        {
            field: 'isIndex',
            title: '是否首页显示',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class=\"label label-primary\">首页显示</span>"
                } else if (value == 0) {
                    return "<span class=\"label label-danger\">首页不显示</span>"
                }
                return "-";
            }
        },
        {
            field: 'isCheck',
            title: '是否审核',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class=\"label label-primary\">已审核</span>"
                } else if (value == 0) {
                    return "<span class=\"label label-danger\">未审核</span>"
                }
                return "-";
            }
        },
        {
            field: 'type',
            title: '类型',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class=\"label label-warning\">客户动态</span>"
                } else if (value == 2) {
                    return "<span class=\"label label-warning\">问答</span>"
                } else if (value == 3) {
                    return "<span class=\"label label-warning\">技师实拍</span>"
                }
                return "-";
            }
        },
        {
            field: 'sort',
            title: '排序',
            align: 'center'
        },
        {
            field: 'gmtCreate',
            title: '创建时间',
            align: 'center',
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },
        {
            field: 'gmtModified',
            title: '更新时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        }
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNo: params.pageNumber,
            pageSize: params.pageSize,
            searchText: params.searchText,
            companyId: companyId,
            isCheck: $("#isCheckS").val(),
            isIndex: $("#isIndexS").val(),
            type: $("#type").val(),
            sortName: params.sortName,      //排序列名
            sortOrder: params.sortOrder //排位命令（desc，asc）
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.user_soundDynamic_list, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': ''
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入作者名字搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['soundDynamic:add', 'soundDynamic:update', 'soundDynamic:delete', 'soundDynamic:list'],//本页权限数组
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        reload: function() {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
        update: function () {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0) {
                layer.msg('请至少选中一条数据', {time: 1500, icon: 5});
                return;
            }
            $("#updatestatusModal").modal("show");
        },
        submit: function () {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');

            var ids = "";
            for (let i = 0; i < list.length; i++) {
                ids += list[i].id;
                if ((i + 1) != list.length) {
                    ids += ",";
                }
            }

            Post(that, _HOST + _API.user_soundDynamic_update + "?ids=" + ids + "&isCheck=" + $("#isCheck").val() + "&isIndex=" + $("#isIndex").val() + "&sort=" + $("#sort").val(), {}, true, '')
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    $('#bootstrapTableEvents').bootstrapTable('refresh');
                    if (response) {
                        if (response.success) {
                            $("#updatestatusModal").modal('hide');
                            layer.msg(response.msg, {time: 1500, icon: 1});
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        //删除
        del: function (index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0) {
                layer.msg('请至少选中一条数据', {time: 1500, icon: 5});
                return;
            }

            var ids = "";
            for (let i = 0; i < list.length; i++) {
                ids += list[i].id;
                if ((i + 1) != list.length) {
                    ids += ",";
                }
            }

            var confirm = layer.confirm('确定删除选中项？', {
                btn: ['确定', '取消'], //按钮
                shadeClose: false,
            }, function () {
                Get(that, _HOST + _API.user_soundDynamic_delete + "?ids=" + ids)
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response);
                        layer.close(confirm);
                        $('#bootstrapTableEvents').bootstrapTable('refresh');
                        if (response) {
                            if (response.success) {
                                layer.msg(response.msg, {time: 1500, icon: 1});
                            } else {
                                layer.msg(response.msg, {time: 1500, icon: 5});
                            }
                        }
                    })
            });
        },
    },
});

$(function () {
    //初始化表格
    tableInit();
});
